﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nbb.darbo_grafikas
{
    class Defektoskopas
    {
        public int Id { get; private set; }
        public string Kodas { get; private set; }
        public string Modelis { get; private set; }
        public string GamNumeris { get; private set; }
        public int GamMetai { get; private set; }

        public Defektoskopas(string kodas, string modelis, string gamNr, int gamMetai, int id=0)
        {
            Id = id;
            Kodas = kodas;
            Modelis = modelis;
            GamNumeris = gamNr;
            GamMetai = gamMetai;
        }

        public Defektoskopas(DataRow dr)
        {
            Id = (int)(dr["Id"]);
            Kodas = (string)dr["Kodas"];
            Modelis = (string)dr["Modelis"];
            GamNumeris = (string)dr["Gam_Nr"];
            GamMetai = ((DateTime)dr["Gam_metai"]).Year;
        }

        public string sqlInsertString()
        {
            return string.Format("('{0}', '{1}', '{2}', {3})", Kodas, Modelis, GamNumeris, GamMetai);
        }

        public string Pavadinimas
        {
            get { return string.Format("{0}, {1} Nr. {2}", Kodas, Modelis, GamNumeris); }
        }

        public bool SameId(Defektoskopas kitasDpas)
        {
            return (this.Id == kitasDpas.Id);
        }

        public bool SameObject(Defektoskopas kitasDpas)
        {
            //Check whether the compared objects reference the same data.
            return !Object.ReferenceEquals(this, null) && Object.ReferenceEquals(kitasDpas, null) && Object.ReferenceEquals(this, kitasDpas);
        }
    }

    // Custom comparer for the Product class
    class DefektoskopasComparer : IEqualityComparer<Defektoskopas>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(Defektoskopas x, Defektoskopas y)
        {

            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;

            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;

            //Check whether the products' properties are equal.
            return x.Id == y.Id;
        }

        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        public int GetHashCode(Defektoskopas defektoskopas)
        {
            //Check whether the object is null
            if (Object.ReferenceEquals(defektoskopas, null)) return 0;

            //Get hash code for the Kodas field if it is not null.
            int hashDefektoskopasKodas = defektoskopas.Kodas == null ? 0 : defektoskopas.Kodas.GetHashCode();

            //Get hash code for the Id field.
            int hashDefektoskopasId = defektoskopas.Id.GetHashCode();

            //Get hash code for the GamNumeris field.
            int hashDefektoskopasGamNr = defektoskopas.GamNumeris.GetHashCode();

            //Get hash code for the GamNumeris field.
            int hashDefektoskopasGamMetai = defektoskopas.GamMetai.GetHashCode();

            //Get hash code for the Modelis field.
            int hashDefektoskopasModelis = defektoskopas.Modelis.GetHashCode();

            //Calculate the hash code for the product.
            return hashDefektoskopasKodas ^ hashDefektoskopasId ^ hashDefektoskopasGamNr ^ hashDefektoskopasGamMetai ^ hashDefektoskopasModelis;
        }

    }
}
